package com.ruoyi.web.controller.employee;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.employee.domain.HrContract;
import com.ruoyi.employee.service.IHrContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同管理Controller
 *
 * @author hango
 * @date 2025-10-19
 */
@RestController
@RequestMapping("/employee/contract")
public class HrContractController extends BaseController
{
    @Autowired
    private IHrContractService hrContractService;

    /**
     * 查询合同管理列表（联表：员工/公司/部门/岗位）
     */
    @PreAuthorize("@ss.hasPermi('employee:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(HrContract hrContract)
    {
        startPage();
        // 使用联表查询，结果包含 empNo、empName、companyName、deptName、postName、termText 等
        List<Map<String, Object>> list = hrContractService.selectHrContractListWithJoin(hrContract);
        return getDataTable(list);
    }

    /**
     * 导出合同管理列表（按实体字段导出）
     */
    @PreAuthorize("@ss.hasPermi('employee:contract:export')")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HrContract hrContract)
    {
        // 导出仍采用实体字段，配合 @Excel 注解更稳定
        List<HrContract> list = hrContractService.selectHrContractList(hrContract);
        ExcelUtil<HrContract> util = new ExcelUtil<>(HrContract.class);
        util.exportExcel(response, list, "合同管理数据");
    }

    /**
     * 获取合同管理详细信息（联表详情）
     */
    @PreAuthorize("@ss.hasPermi('employee:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        // 返回联表详情，包含组织与展示字段
        return success(hrContractService.selectHrContractByIdWithJoin(id));
    }

    /**
     * 新增合同管理
     */
    @PreAuthorize("@ss.hasPermi('employee:contract:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HrContract hrContract)
    {
        return toAjax(hrContractService.insertHrContract(hrContract));
    }

    /**
     * 修改合同管理
     */
    @PreAuthorize("@ss.hasPermi('employee:contract:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HrContract hrContract)
    {
        return toAjax(hrContractService.updateHrContract(hrContract));
    }

    /**
     * 删除合同管理
     */
    @PreAuthorize("@ss.hasPermi('employee:contract:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hrContractService.deleteHrContractByIds(ids));
    }
}
